import request from './instance'
import {
  statusIsPass,
  errorHandle,
  setConfigAuthorization,
  responseErrnoHandler
} from './handers'
request.interceptors.request.use(function (config) {
  setConfigAuthorization(config)
  return config
}, errorHandle)
request.interceptors.response.use(function (res) {
  const data = res.data
  const status = res.status
  const errno = data.errno
  if (statusIsPass(status)) {
    const v = responseErrnoHandler(errno)
    if (v) {
      return Promise.reject(new Error(data.msg))
    } else {
      return data
    }
  } else {
    return Promise.reject(new Error(data.msg))
  }
}, errorHandle)
export default request
